﻿using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.AspNetCore.Mvc;
using System.ComponentModel.Design;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using T.ApiResults;
using T.Materials.API.Application.Command.ExamineAndApproveCommand;
using T.Materials.API.Application.Command.MaterialRequisitionCommand;
using T.Materials.API.Application.Command.MaterialStatisticsCommand;
using T.Materials.API.Application.Command.MaterialStoreCommand;
using T.Materials.API.Application.CommandHandler.MaterialRequisitionCommandHandler;
using T.Materials.API.MaterialsServer;
using T.Materials.Domain.Model;

namespace T.Materials.API.Controllers
{
    /// <summary>
    /// API控制器--物资领用
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class MaterialRequisitionController : ControllerBase
    {
        /// <summary>
        /// 命令总线
        /// </summary>
        protected readonly IMediator mediator;

        /// <summary>
        /// HTTP添加请求
        /// </summary>
        protected readonly HttpPosts httpPosts;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mediator">命令总线</param>
        /// <param name="httpPosts">HTTP添加请求</param>
        public MaterialRequisitionController(IMediator mediator, HttpPosts httpPosts)
        {
            this.mediator = mediator;
            this.httpPosts = httpPosts;
        }

        /// <summary>
        /// 新增物资领用
        /// </summary>
        /// <param name="command">新增物资领用命令</param>
        /// <returns>返回受影响的行数</returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddMaterialRequisition(AddMaterialRequisitionCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                command.CompanyID = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司Id,
                command.User_Id = int.Parse(HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value); //获取用户Id,
                command.Recipient= HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名称
                command.CreatedBy = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取名称
                result.Code = await mediator.Send(command);
            }
            catch (Exception)
            {
                result.Code = -1;
                result.Message = "新增物资领用失败";
                throw;
            }
            return result;
        }

        /// <summary>
        /// 删除物资领用
        /// </summary>
        /// <param name="command">删除物资领用命令</param>
        /// <returns>返回受影响的行数</returns>
        [HttpPost]
        public async Task<ApiResult<int>> DeletedMaterialRequisition(DeletedMaterialRequisitionCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await mediator.Send(command);
                command.DeletedBy = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取名称
            }
            catch (Exception)
            {
                result.Code = -1;
                result.Message = "删除物资领用失败";
                throw;
            }
            return result;
        }


        /// <summary>
        /// 修改物资领用
        /// </summary>
        /// <param name="command">修改物资领用命令</param>
        /// <returns>返回受影响的行数</returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdateMaterialRequisition(UpdateMaterialRequisitionCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {

                command.CompanyID = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司Id,
                command.User_Id = int.Parse(HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value); //获取用户Id,
                command.Recipient = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名称
                command.UpdatedBy = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取名称
                result.Code = await mediator.Send(command);
            }
            catch (Exception)
            {
                result.Code = -1;
                result.Message = "修改物资领用失败";
                throw;
            }
            return result;
        }

        /// <summary>
        /// 批量删除物资领用
        /// </summary>
        /// <param name="command">批量删除物资领用命令</param>
        /// <returns>返回受影响的行数</returns>
        [HttpPost]
        public async Task<ApiResult<int>> BatchDeletedMaterialRequisition(BatchDeletedMaterialRequisitionCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await mediator.Send(command);
                command.DeletedBy = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名称
                //result.Code = await mediator.Send(new BatchDeletedMaterialRequisitionCommand()
                //{
                //    MaterialRequisitionId = command.MaterialRequisitionId,
                //    DeletedBy = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value//获取用户名称
                //});
            }
            catch (Exception)
            {
                result.Code = -1;
                result.Message = "批量删除物资领用失败";
                throw;
            }
            return result;
        }


        /// <summary>
        /// 物资领用审批(通过/拒绝)
        /// </summary>
        /// <param name="command">审批物资入库命令</param>
        /// <returns>返回受影响的行数</returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdateMaterialStatisticsApprovalStatus(MaterialStatisticsApproveCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                command.CompanyID = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司Id,
                command.User_Id = int.Parse(HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value); //获取用户Id,
                command.CreatedBy = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取名称
                command.Approver = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名称

                result.Code = await mediator.Send(command);
            }
            catch (Exception)
            {
                result.Code = -1;
                result.Message = "审批物资入库失败";
                throw;
            }
            return result;
        }

        /// <summary>
        /// 审批物资
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> CrerateAppro(MaterialRequisitionApproveCommand command)
        {
            ApiResult<int> apiResult = new ApiResult<int>();
            try
            {

                command.CompanyID = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司Id,
                command.User_Id = int.Parse(HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value); //获取用户Id,
                command.CreatedBy = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取名称
                command.CreatedDate = DateTime.Now;
                apiResult.Data = await mediator.Send(command);
            }
            catch (Exception ex)
            {
                apiResult.Code = -1;
                apiResult.Message = ex.Message;
                throw;
            }
            return apiResult;
        }

        /// <summary>
        /// 更新物资领用状态
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdadteApprovalStatus(UpdadteMaterialRequisitionStatusCommand command)
        {
            ApiResult<int> apiResult = new ApiResult<int>();
            try
            {
                apiResult.Code = await mediator.Send(command);
            }
            catch (Exception ex)
            {
                apiResult.Code = -1;
                apiResult.Message = ex.Message;
                throw;
            }
            return apiResult;
        }

        /// <summary>
        /// 新增物资领用HTTP请求(当前自己请求自己)
        /// </summary>
        /// <returns>返回受影响的行数</returns>
        [HttpPost]
        public async Task<IActionResult> AddHttpPost()
        {
            try
            {
                return Ok(await httpPosts.CreatePost());
            }
            catch (Exception)
            {

                throw;
            }
        }

    }
}
